package com.xiaoran.robot.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONValidator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoran.robot.entity.Dicts;
import com.xiaoran.robot.mapper.DictsMapper;
import com.xiaoran.robot.service.DictsService;
import com.xiaoran.robot.utils.YamlReader;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DictsServiceImpl implements DictsService {
    @Autowired
    DictsMapper mapper;
    String dictsAPath = YamlReader.instance.getValueByKey("mirai.dictsAPath").toString();
    String dictsBPath = YamlReader.instance.getValueByKey("mirai.dictsBPath").toString();

    @Override
    public int insertOne(Dicts data) {
        return mapper.insert(data);
    }

    @Override
    public List<Dicts> selectList() {
        return mapper.selectList(new QueryWrapper<Dicts>()
        .eq("status", 1));
    }

    @Override
    public List<String> getKeys(){
        List<Dicts> datadicts = selectList();
        List<String> lists = new ArrayList<>();
        for (Dicts dicts : datadicts) {
            lists.add(dicts.getDictKey());
        }
        return lists;
    }

    @Override
    public void dictsToJson() {
        List<Dicts> dicts = selectList();
        HashMap<String, Object> dictsAList = new HashMap<>();
        HashMap<String, Object> dictsBList = new HashMap<>();
        for (Dicts dict : dicts) {
            Integer type = dict.getType();
            String dictKey = dict.getDictKey();
            String dictVal = dict.getDictVal();
            boolean validate = JSONValidator.from(dictVal).validate();
            if(type == 1){
                // 需要条件 => dictsB
                if(validate){
                    dictsBList.put(dictKey, JSON.parseObject(dictVal));
                }else {
                    dictsBList.put(dictKey, dictVal);
                }
            }else{
                if(validate){
                    dictsAList.put(dictKey, JSON.parseObject(dictVal));
                }else {
                    dictsAList.put(dictKey, dictVal);
                }
            }

        }
        String dictsAJson = JSONObject.toJSONString(dictsAList);
        String dictsBJson = JSONObject.toJSONString(dictsBList);
        try {
            FileUtils.forceDelete(new File(dictsAPath));
            FileUtils.forceDelete(new File(dictsBPath));
            FileUtils.writeStringToFile(new File(dictsAPath), dictsAJson, "UTF-8");
            FileUtils.writeStringToFile(new File(dictsBPath), dictsBJson, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Dicts getOne(Integer id) {
        return mapper.selectOne(new QueryWrapper<Dicts>().eq("id", id));
    }

    @Override
    public void delDict(Integer id) {
        mapper.delete(new QueryWrapper<Dicts>().eq("id", id));
    }

}
